package com.zhongzhi.javafx.controller;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import com.fasterxml.jackson.databind.ser.std.StdArraySerializers.IntArraySerializer;
import com.zhongzhi.common.Common;
import com.zhongzhi.common.GrabDetail;
import com.zhongzhi.common.Plc;
import com.zhongzhi.thread.TaskTherad;
import com.zhongzhi.util.ClientUtil;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;

import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.AnchorPane;
import javafx.scene.paint.Paint;
import lombok.extern.log4j.Log4j;
import javafx.scene.image.ImageView;
@Log4j
public class GRADController implements Initializable{
	@FXML
	private AnchorPane AP;
	@FXML
	private ImageView IMAGES;
	@FXML
	private Label ID;
	@FXML
	private Label START;
	@FXML
	private Label AH;
	@FXML
	private Label BH;
	@FXML
	private Label AXH;
	@FXML
	private Label BXH;
	@FXML
	private Button STOP;
	@FXML
	private Button DE;
	//当前
	private GrabDetail grabDetail;
	
	private Timer timer;
	
	@Override
	public void initialize(URL location, ResourceBundle resources) {
		log.info("工位窗口:初始化开始");
		// 获取公共资源
		Plc plc = Common.getPlc();
		Common.addGrup();
		Integer selectStation = Common.getGrup();
		HashMap<Integer, GrabDetail> map = plc.getMap();
		Set<Integer> keySet = map.keySet();
		int a = 0;
		for (Integer integer : keySet) {
			a++;
			if (selectStation==a) {
				grabDetail = map.get(integer);
			}
		}
		
		STOP.setOnAction(new EventHandler<ActionEvent>() {
			
			@Override
			public void handle(ActionEvent event) {
				try {
					String name = grabDetail.getStationName();
					
					String string = "http://127.0.0.1:8080/doosanRadar/ds/enddetection?station=" + name;
					log.info("工位窗口:发起停止任务请求:"+string);
					ClientUtil.Gethttp(string);
				} catch (Exception e) {
					log.error("工位窗口:发起停止任务请求失败:"+e);
				}
				
			}
		});
		
		
		//切换画面按钮
		DE.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				
				//设置从详细界面返回总体画面的编号
				Common.setPage(plc.getId());
				//设置详细界面的挖掘机通道id
				Common.setSelectStation(grabDetail.getId());
				qiehuan();
			}
		});
		if (timer==null) {
			timer = new Timer();
			timer.scheduleAtFixedRate(new TimerTask() {
				@Override
				public void run() {
					Platform.runLater(new Runnable() {
						@Override
						public void run() {
							Ref(grabDetail);
						}
					});
				}
			}, 100, 500);
			Common.getTimelist().add(timer);
			
		}
		log.info("工位窗口:初始化完成");
	}
	//刷新
	private void Ref(GrabDetail grabDetail) {
		Integer stationId = grabDetail.getId();
		TaskTherad taskTherad = Common.getTaskTherads().get(stationId);
		String type = taskTherad.getType();		//机型
		String num = taskTherad.getNum();		//机号
		
		Long startTime = taskTherad.getStartTime();		//开始时间
		Long timeLong = taskTherad.getTimeLong();		//运行时间
		Boolean run = taskTherad.getRun();				//运行状态
		Boolean stop2 = taskTherad.getStop();			//强行停止
		Long resTime = taskTherad.getResTime();			//剩余时间
		
		Double startHA = taskTherad.getStartHA();		//开始
		Double CurHA = taskTherad.getCurHA();			//当前
		Double DisHA = taskTherad.getDisHA();			//下落
		String StateA = taskTherad.getStateA();			//范围
		
		Double startHB = taskTherad.getStartHB();
		Double CurHB = taskTherad.getCurHB();
		Double DisHB = taskTherad.getDisHB();
		String StateB = taskTherad.getStateB();
		
		
		SetText(ID,grabDetail.getStationName());
		SetTextColour(START, run, "测试中", "停止", "停止");
		
		if (CurHA!=null) {
			String string = String.format("%.1f", CurHA);
			SetText(AH,string);
		}
		if (CurHB!=null) {
			String string = String.format("%.1f", CurHB);
			SetText(BH,string);
		}
		if (DisHA!=null) {
			String string = String.format("%.1f", DisHA);
			SetText(AXH,string);
		}
		if (DisHB!=null) {
			String string = String.format("%.1f", DisHB);
			SetText(BXH,string);
		}
		
	}

	//设置文字
	private void SetText(Label label,String s) {
		if (s==null) {
			label.setText("暂无");
		}else {
			label.setText(s);
		}
	}

	//设置文字和颜色
	private void SetTextColour(Label label,Boolean bool,String run,String stop,String open) {

		if (bool==null) {
			label.setText(open);
			label.setTextFill(Paint.valueOf("#000000"));
		}else if (bool==true) {
			label.setText(run);
			label.setTextFill(Paint.valueOf("#00cf00"));
		}else{
			label.setText(stop);
			label.setTextFill(Paint.valueOf("#cf0000"));
		}
	}

	private void qiehuan() {
		try {
			ScrollPane sp = Common.getSp();
			log.info("工位窗口:切换到详细画面,工位ID:"+grabDetail.getId());
			sp.setContent(FXMLLoader.load(getClass().getResource("/fxml/run/detail.fxml")));
		} catch (IOException e) {
			log.info("工位窗口:切换到详细画面失败:"+e);
		}
	}

}
